package api

import (
	"encoding/json"
	"fmt"
	"time"
	"weiboxs.com/new_h_glasses/utils"

	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
)

type ApiOptometryController struct {
	ApiBaseController
}

func (c *ApiOptometryController) Prepare() {
	c.ApiBaseController.Prepare()
	c.CtrLoginToken()
}

func (c *ApiOptometryController) ApiDataGrid() {
	c.dataGrids()
}

// 通用列表数据
func (c *ApiOptometryController) dataGrids() {
	var params models.OptometryQueryParam
	filter := c.GetString("keyword")
	arch_id, _ := c.GetInt64("arch_id")
	limit, _ := c.GetInt("limit", 20)
	offset, _ := c.GetInt64("offset", 0)

	params.Filter = filter
	params.Limit = limit
	params.Offset = offset
	params.ArchivesId = arch_id
	params.Depid = c.curUser.DepartmentId

	data, total := models.OptometryPageList(&params)
	c.jsonResultData(enums.JRCodeSucc, total, data)
}

// 编辑数据
func (c *ApiOptometryController) ApiEdit() {
	c.checkAuthor("optometry/index")
	arch_id, _ := c.GetInt64("arch_id")
	doctor := c.GetString("doctor")
	optometrist := c.GetString("optometrist")
	remarks := c.GetString("remarks")
	chufang := c.GetString("chufang")
	if arch_id == 0 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}
	m := new(models.Optometry)
	m.DepartmentId = c.curUser.DepartmentId
	m.CreatedAt = time.Now().Unix()
	m.CreatedBy = c.curUser.RealName
	m.UpdatedBy = c.curUser.RealName
	m.UpdatedAt = time.Now().Unix()
	m.Archives = &models.Archives{Id: arch_id}
	m.Remarks = remarks
	m.Doctor = doctor
	m.Optometrist = optometrist
	m.Chufang = chufang
	m.DocumentNumber = models.CreateOptometryDocumentNumber(&c.curUser)
	if _, err := models.OptometryAdd(m); err != nil {
		c.jsonResult(enums.JRCodeFailed, "验光处方保存失败，请重试", "")
		return
	}
	c.jsonResult(enums.JRCodeSucc, "验光处方保存成功", "")
}

// 编辑数据（单例）
func (c *ApiOptometryController) ApiEditSingle() {
	c.checkAuthor("optometry/index")
	id, _ := c.GetInt64("opt_id") //验光ID
	if id == 0 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}
	doctor := c.GetString("doctor")
	optometrist := c.GetString("optometrist")
	remarks := c.GetString("remarks")
	chufang := c.GetString("chufang")
	m, err := models.GetOptometryById(id)
	if err != nil {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}
	if m.DepartmentId != c.curUser.DepartmentId {
		c.jsonResult(enums.JRCodeFailed, "数据无效", "")
	}
	m.UpdatedBy = c.curUser.RealName
	m.UpdatedAt = time.Now().Unix()
	m.Remarks = remarks
	m.Doctor = doctor
	m.Optometrist = optometrist

	m.Chufang = chufang
	if err := m.OptometryEdit(); err != nil {
		c.jsonResult(enums.JRCodeFailed, "验光信息保存失败，请重试", "")
		return
	}
	c.jsonResult(enums.JRCodeSucc, "验光处方保存成功", "")
}

func (c *ApiOptometryController) createDocumentNumber() string {
	var documentNumber string = ""
	stat := utils.GetDateParse(time.Now().Format("2006-01-02"))
	end := stat + 86400
	documentNumber = fmt.Sprintf("%s%04d", time.Now().Format("20060102150405"), models.QueryOptometryTimeSoltSeterCount(&c.curUser, stat, end)+1)
	return documentNumber
}

// 获取配置信息
func (c *ApiOptometryController) GetOption() {
	c.checkAuthor("optometry/index")
	o := make(map[string]interface{})
	f := models.OptionsQueryParam{}
	f.Status = "1"
	f.Depid = c.curUser.DepartmentId
	//获取医生
	f.Typeid = "10"
	o["doctor"] = models.OptionsList(&f)

	//f.Typeid = "11"
	//o["optometrist"] = models.OptionsList(&f)

	c.jsonResult(enums.JRCodeSucc, "ok", o)
}

// 删除
func (c *ApiOptometryController) ApiDelete() {
	c.checkAuthor("optometry/delete")
	id, err := c.GetInt64("id")
	if err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}

	if err := models.OptometryDelete(id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "信息删除成功", "")
}

// 查看验光数据
func (c *ApiOptometryController) ApiView() {
	c.checkAuthor("optometry/index")

	if c.Ctx.Input.IsPost() {
		var params models.OptometryQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		data := models.OptometryList(&params)
		c.jsonResultData(enums.JRCodeSucc, 10000, data)
	}
}

// 查看单条验光数据,JSON返回
func (c *ApiOptometryController) ApiViewOne() {
	//c.checkAuthor("optometry/index")

	if c.Ctx.Input.IsPost() {
		optId, _ := c.GetInt64("id") //档案ID
		data, err := models.GetOptometryById(optId)
		if err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		data.ChufangData = models.GetOptometryDataList(optId)
		c.jsonResult(enums.JRCodeSucc, "获取成功", data)
	}
	c.jsonResult(enums.JRCodeFailed, "获取指定参数出错！", "")
}

// 调取患者验光数据
func (c *ApiOptometryController) OptometryDataLists() {
	if c.Ctx.Input.IsPost() {
		archives_id, _ := c.GetInt64("archives_id")
		if archives_id == 0 {
			c.jsonResult(enums.JRCodeFailed, "参传错误", "")
		}
		
		var params models.OptometryQueryParam
		params.ArchivesId = archives_id
		lists := models.OptometryList(&params)
		if len(lists) > 0 {
			for k, _ := range lists {
				lists[k].Archives = nil
				lists[k].CreatedBy = fmt.Sprintf("%s / %s", utils.GetDate(lists[k].CreatedAt), lists[k].CreatedBy)
				lists[k].ChufangData = models.GetOptometryDataList(lists[k].Id)
			}
		}
		c.jsonResult(enums.JRCodeSucc, "ok", lists)
	}
	c.jsonResult(enums.JRCodeFailed, "参数错误", "")
}
